package net.sxnic.item.print.word;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import net.sxnic.comm.basecode.BaseCodeUtils;
import net.sxnic.comm.utils.WordUtils;
import net.sxnic.item.ItemConstants;
import net.sxnic.item.mdata.Mdata;
import net.sxnic.jhitem.itemannex.ItemAnnex;
import net.sxnic.jhitem.itemclob.ItemClob;
import net.sxnic.jhitem.itemfund.ItemFund;
import net.sxnic.jhitem.itempeople.ItemPeople;
import net.sxnic.jhitem.itemprincipal.ItemPrincipal;
import net.sxnic.jhitem.itemssmb.ItemSsmb;
import net.sxnic.taglib.BaseCodeUtilsCheckbox;

import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.ImportFormatMode;

/**
 * 项目基本信息
 * 
 * @author CGD
 * 
 */
public class KjggInfo {
	static {
		Document.setLicence("sj_laokai");
	}
	// 实施目标索引
	private static int SSMBROWINDEX = 24;
	private static int TABLEINDEXSSMB = 1;
	private static int SSMBROWMAX = 5;
	// 研究人员索引
	private static int PEOPLEROWINDEX = 67;
	private static int TABLEINDEXPEOPLE = 1;
	private static int PEOPLEROWMAX = 10;

	public static void createWord(String saveDocFile, String templateFile,
			Mdata mdata, ItemFund fund, ItemPrincipal principal, ItemClob clob,
			List<ItemPeople> peoples, List<ItemSsmb> ssmbs,
			List<ItemAnnex> annex002List, String basePath) {
		Document doc;
		try {
			File file = new File(saveDocFile);
			if (!file.getParentFile().exists()
					&& !file.getParentFile().mkdirs()) {
				return;
			}

			doc = new Document(templateFile);
			DocumentBuilder builder = new DocumentBuilder(doc);
			SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
			BaseCodeUtilsCheckbox bcuc = new BaseCodeUtilsCheckbox();
			// 设置标签
			WordUtils.setBookmarks(doc, mdata);
			WordUtils.setBookmarks(doc, fund);
			WordUtils.setBookmarks(doc, clob);
			WordUtils.setBookmarks(doc, principal);

			WordUtils.setBookmark(doc, "appYear", mdata.getAppYear());
			WordUtils.setBookmark(doc, "appYearNext",
					String.valueOf(Integer.valueOf(mdata.getAppYear()) + 1));
			WordUtils.setBookmark(doc, "appYearAfterNext",
					String.valueOf(Integer.valueOf(mdata.getAppYear()) + 2));
			WordUtils.setBookmark(doc, "sortType",
					BaseCodeUtils.getInfoName("009", mdata.getSortType()));
			WordUtils.setBookmark(doc, "guideField",
					BaseCodeUtils.getInfoName("017", mdata.getGuideField()));
			WordUtils.setBookmark(doc, "industry",
					BaseCodeUtils.getInfoName("053", mdata.getIndustry()));
			WordUtils.setBookmark(doc, "product",
					BaseCodeUtils.getInfoName("052", mdata.getProduct()));
			WordUtils.setBookmark(doc, "expertPantent",
					BaseCodeUtils.getInfoName("043", mdata.getExpertPantent()));
			WordUtils.setBookmark(doc, "unitType",
					BaseCodeUtils.getInfoName("028", mdata.getUnitType()));
			if(StringUtils.isNotBlank(mdata.getCheckContent()))
                 WordUtils.setBookmark(doc, "total",String.valueOf(mdata.getCheckContent().split(",").length));
			else
				WordUtils.setBookmark(doc, "total","0");
			if (StringUtils.isNotBlank(mdata.getZzdwCode())) {
				WordUtils.setBookmark(
						doc,
						"zzdwCode",
						ItemConstants.ZZDW_MAP.get(
								ItemConstants.SYSTEM_CALALOG_ALIAS).get(
								mdata.getZzdwCode()));
			}
			WordUtils.setBookmark(doc, "soperCode",
					ItemConstants.OPERATION_MAP.get(mdata.getSoperCode()));
			if (StringUtils.isNotBlank(mdata.getCheckContent())) {
				WordUtils.setBookmark(doc, "checkContent",
						bcuc.splitInfoCode("021", mdata.getCheckContent()));
			}
			if (StringUtils.isNotBlank(mdata.getIndustry())) {
				WordUtils.setBookmark(doc, "industry",
						bcuc.splitInfoCode("053", mdata.getIndustry()));
			}

			// String temp = mdata.getCheckContent();
			// bcuc.setBookmarkList(doc,"checkContent","021", list);

			/*
			 * for(int i=0;i<list.length;i++) { WordUtils.setBookmark(doc,
			 * "checkContent"+i, BaseCodeUtils.getInfoName("021",
			 * list[i].trim())); }
			 */

			/*
			 * for(int i=0;i<list.length;i++){ System.out.println(list[i]); }
			 * System.out.println("111111111111111111111111111111111111111111");
			 */

			// 项目分阶段具体实施目标

			if (ssmbs != null && ssmbs.size() > 0) {
				builder.moveToBookmark("ssmb_table");

				int tableIndex = TABLEINDEXSSMB;
				int j = SSMBROWINDEX;// 开始表格Row, 这里的index从0开始

				for (ItemSsmb ssmb : ssmbs) {
					// 实施时间区间
					builder.moveToCell(tableIndex, j, 0, 0);
					builder.write(ssmb.getStime() + "至" + ssmb.getEtime());
					// 实施内容
					builder.moveToCell(tableIndex, j, 1, 0);
					builder.write(ssmb.getContent());
					j++;
					if ((j - SSMBROWINDEX) >= SSMBROWMAX) {
						break;
					}
				}
			}
			// 主要研究人员
			if (null != peoples && peoples.size() > 0) {
				builder.moveToBookmark("designer");
				int j = PEOPLEROWINDEX;

				for (ItemPeople p : peoples) {
					builder.moveToCell(TABLEINDEXPEOPLE, j, 0, 0);
					builder.write(p.getChinName());
					builder.moveToCell(TABLEINDEXPEOPLE, j, 1, 0);
					builder.write(BaseCodeUtils.getInfoName("002", p.getSex()));
					builder.moveToCell(TABLEINDEXPEOPLE, j, 2, 0);
					builder.write(p.getBorn());
					builder.moveToCell(TABLEINDEXPEOPLE, j, 3, 0);
					builder.write(BaseCodeUtils.getInfoName("006",
							p.getEducate()));
					builder.moveToCell(TABLEINDEXPEOPLE, j, 4, 0);
					builder.write(BaseCodeUtils.getInfoName("024",
							p.getScienceTitle()));
					builder.moveToCell(TABLEINDEXPEOPLE, j, 5, 0);
					builder.write(p.getSpecialty());
					builder.moveToCell(TABLEINDEXPEOPLE, j, 6, 0);
					builder.write(p.getTask());
					builder.moveToCell(TABLEINDEXPEOPLE, j, 7, 0);
					builder.write(p.getUnit());

					j++;

					if ((j - PEOPLEROWINDEX) >= PEOPLEROWMAX) {
						break;
					}
				}
			}
			// 文档合并
			if (annex002List != null && annex002List.size() > 0) {
				for (ItemAnnex itemAnnex : annex002List) {
					doc.appendDocument(new Document(itemAnnex.getFilePath()
							+ itemAnnex.getAttachName()),
							ImportFormatMode.KEEP_SOURCE_FORMATTING);
				}

			}
			doc.save(saveDocFile);
		} catch (Exception ex) {
			ex.printStackTrace();
			try {
				doc = new Document(templateFile);
				doc.save(saveDocFile);
			} catch (Exception e) {
				e.fillInStackTrace();
			}
		}
	}

}
